import {
  debounce,
  throttle
} from '../../../utils/util'

import {
  getKeywordsSuggestList
} from '../../../api/home'

import Toast from 'tdesign-miniprogram/toast/index';
let cardList
let userList
let modelList
let electionList
let caseList
let goodsList
const {
  globalData: {
    IMGBASEURL
  }
} = getApp()

Page({
  data: {
    IMGBASEURL,
    pageLoading: false,
    isShowHistory: true,
    isFocus: false,
    isShow: false,
    historyList: [],
    Keywords: '',
    placeholder: "搜索标签、内容、用户",
    tagActive: 1,
    pageTab: [{
        icon: "page-icon-lg.svg",
        text: '灵感',
        tagActive: 1
      },
      {
        icon: "page-icon-mx.svg",
        text: '模型',
        tagActive: 2
      },
      {
        icon: "page-icon-yh.svg",
        text: '用户',
        tagActive: 3
      },
      {
        icon: "page-icon-jx.svg",
        text: '竞选',
        tagActive: 4
      },
      {
        icon: "page-icon-al.svg",
        text: '案例',
        tagActive: 5
      },
      {
        icon: "page-icon-hw.svg",
        text: '好物',
        tagActive: 6
      },
    ],
    tagListOne: [{
        text: '灵感',
        val: 1
      },
      {
        text: '模型',
        val: 2
      },
      {
        text: '用户',
        val: 3
      },
      {
        text: '竞选',
        val: 4
      },
      {
        text: '案例',
        val: 5
      },
      {
        text: '好物',
        val: 6
      },
    ],
    isModel: 0, //模型广场
    keywordsList: [], //关键字展示列表
    realKeywordsList: [],
  },
  // 点击icon图标
  clickIcon(e) {
    console.log('点击icon图标', e.currentTarget.dataset.item);
    let item = e.currentTarget.dataset.item
    this.setData({
      tagActive: item.tagActive,
    })
    this.onSearch()
  },
  chlichTap(e) {
    const index = e.currentTarget.dataset['index']
    this.setData({
      tagActive: index,
      keywordsList: [],
      realKeywordsList: [],
    })
    this.datasLoad(true)
  },
  clear() {
    this.setData({
      Keywords: '',
      isShowHistory: true,
      keywordsList: [],
      realKeywordsList: [],
      tagActive: 1
    })
  },
  onReachBottom() {
    console.log();
    if (this.data.tagActive == 1) {
      cardList.onReachBottom(false, this.data.Keywords)
    }
    if (this.data.tagActive == 2) {
      modelList.onReachBottom(false, this.data.Keywords)
    }
    if (this.data.tagActive == 3) {
      userList.onReachBottom(false, this.data.Keywords)
    }
    if (this.data.tagActive == 4) {
      electionList.onReachBottom(false, this.data.Keywords)
    }
    if (this.data.tagActive == 5) {
      caseList.onReachBottom(false, this.data.Keywords)
    }
    if (this.data.tagActive == 6) {
      goodsList.onReachBottom(false, this.data.Keywords)
    }
  },
  onInput: debounce(function (even) {
    this.setData({
      Keywords: even.detail.value
    })
    if (!even.detail.value) {
      this.setData({
        isShowHistory: true
      })
    }
    getKeywordsSuggestList({
      Keyword: even.detail.value
    }).then(res => {
      this.setData({
        keywordsList: res.Result.map(item => {
          return item.replace(even.detail.value, `<span class="text_ellipsis" style='color: #fff;'>${even.detail.value}</span>`)
        }),
        realKeywordsList: res.Result
      })
    })
  }, 300),
  // 选中关键字
  selectKeywords(e) {
    this.setData({
      Keywords: this.data.realKeywordsList[e.currentTarget.dataset.index],
      keywordsList: [],
      realKeywordsList: []
    })
    this.onSearch()
  },
  // 输入框失焦
  onInptFocusAandBlur(even) {
    this.setData({
      isFocus: even.type == "focus"
    })
  },
  hisSearch(even) {
    this.setData({
      Keywords: even.currentTarget.dataset['key'],
      keywordsList: [],
      realKeywordsList: []
    })
    this.onSearch()
  },
  onSearch() {
    this.setData({
      Keywords: this.data.Keywords && this.data.Keywords.trim(),
      keywordsList: [],
      realKeywordsList: []
    })
    // 保存到历史记录
    if (this.data.Keywords) {
      let newArr = this.data.historyList.filter(e => e != this.data.Keywords)
      newArr.unshift(this.data.Keywords)
      wx.setStorageSync('historySting', newArr.join('^;^'))
      this.setData({
        historyList: newArr,
        isShowHistory: false
      })
    }
    this.setData({
      isShowHistory: false
    })
    this.datasLoad(true)
  },
  datasLoad(isF = false) {
    if (this.data.tagActive == 1) {
      cardList.loadData(isF, this.data.Keywords)
    }
    if (this.data.tagActive == 2) {
      modelList.loadData(isF, this.data.Keywords)
    }
    if (this.data.tagActive == 3) {
      userList.loadData(isF, this.data.Keywords)
    }
    if (this.data.tagActive == 4) {
      electionList.loadData(isF, this.data.Keywords)
    }
    if (this.data.tagActive == 5) {
      caseList.loadData(isF, this.data.Keywords)
    }
    if (this.data.tagActive == 6) {
      goodsList.loadData(isF, this.data.Keywords)
    }
  },
  qingkong() {
    wx.removeStorageSync('historySting')
    this.setData({
      historyList: []
    })
  },
  onLoad(options) {
    //从模型广场来
    if (options.modelType) this.setData({
      isModel: Number(options.modelType)
    })
    cardList = this.selectComponent('#cardList');
    userList = this.selectComponent('#userList');
    modelList = this.selectComponent('#modelList');
    electionList = this.selectComponent('#electionList');
    caseList = this.selectComponent('#caseList');
    goodsList = this.selectComponent('#goodsList');
  },
  onReady() {
    const Hlist = wx.getStorageSync('historySting')
    if (Hlist) {
      this.setData({
        historyList: Hlist.split('^;^')
      })
    }
  },
  onPullDownRefresh() {
    if (!this.data.isShowHistory) {
      this.setData({
        pageLoading: true,
      });
      this.datasLoad(true)
    } else {
      wx.stopPullDownRefresh();
    }
  },
  // 监听页面滚动
  onPageScroll(e) {
    let scrollTop = parseInt(e.scrollTop)
    if (scrollTop > 200) {
      this.setData({
        isShow: true
      })
    } else {
      this.setData({
        isShow: false
      })
    }
  },
  // 回到顶部
  goTop() {
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  }
})